package com.lzh3.preparedstatement.crud;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.junit.Test;

import com.liuzhihao3.util.JDBCUtils;
import com.lzh3.bean.Customers;

/**
 * 针对于Customers表的查询操作
 * @author Dell
 *
 */
public class CustomersForQuery {
	
	
	
	
	@Test
	public void testQueryForCustomers() {
		String sql = "select id,name,birth,email from customers where id = ?";
		Customers customers = queryForCustomers(sql, 13);
		System.out.println(customers);
		
		String sql1 = "select name,email from customers where name = ?";
		Customers customers2 = queryForCustomers(sql1, "周杰伦");
		System.out.println(customers2);
	}
	/**
	 * 针对于customers表的通用的查询操作
	 * 
	 * @author liuzhihao
	 * @throws Exception 
	 * @date 2021年4月20日下午9:00:54
	 */
	public Customers queryForCustomers(String sql,Object...args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();
			
			ps = conn.prepareStatement(sql);
			for(int i =0; i < args.length;i++) {
				ps.setObject(i+1, args[i]);
			}
			
			rs = ps.executeQuery();
			//获取结果集的元数据
			ResultSetMetaData rsmd = rs.getMetaData();
			//通过ResultSetMetaData获取结果集中的列数
			int columnCount = rsmd.getColumnCount();
			if(rs.next()) {
				Customers cust = new Customers();
				//处理结果集一行数据中的每一列
				for(int i=0;i<columnCount;i++) {
					//获取列值
					Object columValue = rs.getObject(i + 1);
					
					//获取每个列的列名
//					String columnName = rsmd.getColumnName(i + 1);
					String columnLabel = rsmd.getColumnLabel(i + 1);
					
					//给cust对象指定的columnName属性，赋值为columValue:通过反射
					Field field = Customers.class.getDeclaredField(columnLabel);
					field.setAccessible(true);
					field.set(cust, columValue);
				}
				return cust;
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource(conn, ps, rs);
		}
		
		return null;
		
	}
	
	@Test
	public void testQuery1() {
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet resultSet = null;
		try {
			conn = JDBCUtils.getConnection();
			String sql = "select id,name,email,birth from customers where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setObject(1, 1);
			
			//执行并返回结果集
			resultSet = ps.executeQuery();
			//处理结果集
			if(resultSet.next()) {//next():判断结果集的吓一跳是否有数据，如果有数据返回true,并指针下移；如果返回false,指针不会下移
				
				//获取当前这条数据的各个字段值
				int id = resultSet.getInt(1);
				String name = resultSet.getString(2);
				String email = resultSet.getString(3);
				Date birth = resultSet.getDate(4);
				
				//方式一：
//			System.out.println("id = "+id+",name = "+name+",email = "+email+",birth = "+birth);
				
				//方式二：
//			Object[] data = new Object[] {id,name,email,birth};
				
				//方式三：将数据封装为一个对象（推荐）
				Customers customers = new Customers(id,name,email,birth);
				System.out.println(customers);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JDBCUtils.closeResource(conn, ps, resultSet);
		
		
	}
	
}
